package com.eight.homepage.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.TbZoneCourse;
import com.eight.common.pojo.ZoneCourse;
import com.eight.homepage.dao.TbZoneCourseDao;
import com.eight.homepage.dao.ZoneCourseDao;
import com.eight.homepage.service.ZoneCourseService;
import com.eight.homepage.vo.ZoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/29 18:51
 */
@Service
public class ZoneCourseServiceImpl implements ZoneCourseService {

    @Autowired
    private ZoneCourseDao zoneCourseDao;

    @Autowired
    private TbZoneCourseDao tbZoneCourseDao;
    @Override
    public List<ZoneCourse> select(String id) {
        List<ZoneCourse> zoneCourseList = zoneCourseDao.findCourseByZoneId(id);

        return zoneCourseList;
    }

    @Override
    public Page<ZoneCourse> findPage(Integer currentPage, Integer pageSize, ZoneVo zoneVo) {
        Page<ZoneCourse> page = new Page<>(currentPage,pageSize);

        QueryWrapper<ZoneCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("tzc.is_deleted",0);
        wrapper.eq("tz.id",zoneVo.getZoneId());
        wrapper.eq("status","Normal");
        if (StringUtils.hasText(zoneVo.getCourseTitle())) {
            wrapper.like("ec.title", zoneVo.getCourseTitle());
        }
        if(zoneVo.getIsDisable()!=null){
            wrapper.eq("tzc.is_disable",zoneVo.getIsDisable());
        }

        Page<ZoneCourse> allCourse = zoneCourseDao.findAllCourse(page, wrapper);

        return allCourse;
    }

    @Override
    public Page<ZoneCourse> findCoursePage(Integer currentPage, Integer pageSize, ZoneVo zoneVo) {

        //先查出来专区里已经上传的课程
        QueryWrapper<TbZoneCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("zone_id",zoneVo.getZoneId());
        wrapper.eq("is_deleted",0);
        List<TbZoneCourse> tbZoneCourses = tbZoneCourseDao.selectList(wrapper);
        //取出他们的课程id
        List<String> collect = tbZoneCourses.stream().map(item -> item.getCourseId()).collect(Collectors.toList());

        //查出所有的课程
        QueryWrapper<ZoneCourse> wrapper1 = new QueryWrapper<>();
        if (StringUtils.hasText(zoneVo.getTitle())) {
            wrapper1.like("ec.title", zoneVo.getTitle());
        }
        if(StringUtils.hasText(zoneVo.getStatus())){
            wrapper1.eq("ec.status",zoneVo.getStatus());
        }
        wrapper1.eq("ec.is_deleted",0);
        Page<ZoneCourse> page = new Page<>(currentPage, pageSize);
        Page<ZoneCourse> allCourse1 =zoneCourseDao.findAllZoneCourse(page,wrapper1);

        //查出来所有课程中没有添加的课程名
        List<ZoneCourse> collect1 = allCourse1.getRecords().stream().filter(item -> collect.stream().noneMatch(id -> Objects.equals(item.getId(), id)))
                .collect(Collectors.toList());
        //现将所有的都设置为已添加
        for (ZoneCourse eduCourse : allCourse1.getRecords()) {
            eduCourse.setIsAdd(1);
        }
        //再将没有添加的课程设置为未添加
        for (ZoneCourse eduCourse : collect1) {
            eduCourse.setIsAdd(0);
        }

        return allCourse1;


    }


    @Override
    public int delete(String  id) {

        QueryWrapper<ZoneCourse> wrapper = new QueryWrapper<>();
        if(id != null){
            wrapper.eq("id",id);
        }
        int i = zoneCourseDao.delete(wrapper);

        return i;
    }



    //前端页面未进行渲染
    @Override
    public int updateStatus(String zcId, Integer tzcisDisable) {

        int status = zoneCourseDao.updateStatus(zcId, tzcisDisable);
        return status;

    }

    @Override
    public int addCourse(TbZoneCourse tbZoneCourse) {
        TbZoneCourse tbZoneCourse1 = new TbZoneCourse();
        tbZoneCourse1.setId(IdUtil.getSnowflakeNextIdStr());
        tbZoneCourse1.setCourseId(tbZoneCourse.getCourseId());
        tbZoneCourse1.setZoneId(tbZoneCourse.getZoneId());
        tbZoneCourse1.setGmtCreate(LocalDateTime.now());
        tbZoneCourse1.setGmtModified(LocalDateTime.now());
        tbZoneCourse1.setIsDeleted(0);
        tbZoneCourse1.setIsDisable(0);
        return tbZoneCourseDao.insert(tbZoneCourse1);
    }
}
